package com.sunny.ihealth.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.PowerManager;
import android.util.Log;

import java.util.Calendar;

/**
 * 睡眠监测
 * Created by Sunny  An on 2016/6/6.
 */
public class SleepTimeMonitor {
    /**
     * 屏幕状态改变
     */
    public interface ScreenStateChangeListener {
        void onScreenOn();

        void onScreenOff();

        void onUserPresent();
    }

    /**
     * 睡眠时间改变
     */
    public interface OnSleepTimeChangedListener {
        void onSleepTimeChanged(int sleepTime);
    }

    //3小时
    private static final int MIN_SLEEP_TIME = 60 * 3;
    private static final String TAG = "TAG::SleepTimeMonitor";

    private Context context;
    private ScreenReceiver screenReceiver;
    private ScreenStateChangeListener screenStateChangeListener;
    private OnSleepTimeChangedListener onSleepTimeChangedListener;

    private boolean isValid;
    private long startSleepTime, wakeupTime;
    private int totalSleepTime;

    public SleepTimeMonitor(Context context) {
        this.context = context;
        screenReceiver = new ScreenReceiver();
    }

    private class ScreenReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_SCREEN_ON.equals(action)) { // 开屏
                onScreenOn();
            } else if (Intent.ACTION_SCREEN_OFF.equals(action)) { // 锁屏
                onScreenOff();
            } else if (Intent.ACTION_USER_PRESENT.equals(action)) { // 解锁
                onUserPresent();
            }
        }
    }

    /**
     * 获取屏幕状态
     */
    private void getScreenState() {
        PowerManager manager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        if (manager.isScreenOn()) {
            onScreenOn();
        } else {
            onScreenOff();
        }
    }

    public void register() {
        startSleepTime = wakeupTime = 0;
        totalSleepTime = 0;

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        context.registerReceiver(screenReceiver, filter);
        getScreenState();
    }

    public void unRegister() {
        context.unregisterReceiver(screenReceiver);
        screenReceiver = null;
    }


    public ScreenStateChangeListener getScreenStateChangeListener() {
        return screenStateChangeListener;
    }

    public void setScreenStateChangeListener(ScreenStateChangeListener screenStateChangeListener) {
        this.screenStateChangeListener = screenStateChangeListener;
    }

    public OnSleepTimeChangedListener getOnSleepTimeChangedListener() {
        return onSleepTimeChangedListener;
    }

    public void setOnSleepTimeChangedListener(OnSleepTimeChangedListener onSleepTimeChangedListener) {
        this.onSleepTimeChangedListener = onSleepTimeChangedListener;
    }

    /**
     * 开屏
     */
    private void onScreenOn() {
        //Log.e(TAG, "onScreenOn");
        if (screenStateChangeListener != null) {
            screenStateChangeListener.onScreenOn();
        }
    }

    /**
     * 锁屏
     */
    private void onScreenOff() {
        //Log.e(TAG, "onScreenOff");
        if (screenStateChangeListener != null) {
            screenStateChangeListener.onScreenOff();
        }

        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        isValid = (hour >= 21 && hour <= 23 || hour >= 0 && hour <= 5);
        if (isValid) {
            startSleepTime = calendar.getTimeInMillis();
        }
    }


    /**
     * 解锁
     */
    private void onUserPresent() {
        //Log.e(TAG, "onUserPresent");
        if (screenStateChangeListener != null) {
            screenStateChangeListener.onUserPresent();
        }

        if (isValid) {
            Calendar calendar = Calendar.getInstance();
            wakeupTime = calendar.getTimeInMillis();
            int keep = (int) ((wakeupTime - startSleepTime) / (1000 * 60));  //转换成分钟
            if (keep >= MIN_SLEEP_TIME) {   //睡眠时间有效
                totalSleepTime = keep;
                if (onSleepTimeChangedListener != null) {
                    onSleepTimeChangedListener.onSleepTimeChanged(totalSleepTime);
                }
            }
        }
    }


}
